import { Injectable } from '@angular/core';
import { RestService } from '@abp/ng.core';
import { Observable, BehaviorSubject } from 'rxjs';

export interface NotificationOrder {
  id?: string;
  notificationOrderNo: string;
  customerCode: string;
  customerName: string;
  notificationStatus: string;
  productCode: string;
  productName: string;
  notificationQty: number;
  receivedQty: number;
  unit: string;
  volumeCm3: number;
  weightKg: number;
  customerOrderNo: string;
  baseUnitQty: number;
  baseUnit: string;
  productionDate: Date;
}

export interface NotificationOrderFilter {
  maxResultCount?: number;
  skipCount?: number;
  sorting?: string;
  notificationOrderNo?: string;
  customerCode?: string;
  customerName?: string;
  notificationStatus?: string;
  productCode?: string;
  productName?: string;
  customerOrderNo?: string;
}

@Injectable({
  providedIn: 'root'
})
export class NotificationOrderService {
  private readonly apiUrl = '/api/app/notification-order';
  private refreshSubject = new BehaviorSubject<void>(null);
  refresh$ = this.refreshSubject.asObservable();

  constructor(private restService: RestService) {}

  getList(params: NotificationOrderFilter): Observable<any> {
    return this.restService.request({
      method: 'GET',
      url: this.apiUrl,
      params
    });
  }

  getById(id: string): Observable<NotificationOrder> {
    return this.restService.request({
      method: 'GET',
      url: `${this.apiUrl}/${id}`
    });
  }

  create(body: NotificationOrder): Observable<NotificationOrder> {
    return this.restService.request({
      method: 'POST',
      url: this.apiUrl,
      body
    });
  }

  update(id: string, body: NotificationOrder): Observable<NotificationOrder> {
    return this.restService.request({
      method: 'PUT',
      url: `${this.apiUrl}/${id}`,
      body
    });
  }

  delete(id: string): Observable<void> {
    return this.restService.request({
      method: 'DELETE',
      url: `${this.apiUrl}/${id}`
    });
  }

  triggerRefresh(): void {
    this.refreshSubject.next();
  }

  getListByQuery(params: NotificationOrderFilter): Observable<any> {
    return this.restService.request({
      method: 'GET',
      url: this.apiUrl + '/query-notification-orders',
      params
    });
  }
} 